﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Globalization;
using System.Text;

namespace TableParsers
{
    [Serializable]
    public class ClassTime: IEquatable<ClassTime>, IComparable<ClassTime>, IComparable
    {
        TimeSpan m_begin = new TimeSpan(0,0,0);
        public TimeSpan Begin
        {
            get
            {
                return m_begin;
            }
        }

        TimeSpan m_end = new TimeSpan(0, 0, 0);
        public TimeSpan End
        {
            get
            {
                return m_end;
            }
        }

        DateTime m_date = new DateTime();
        public DateTime Date
        {
            get
            {
                return m_date;
            }
        }

        public ClassTime(DateTime date, TimeSpan begin, TimeSpan end)
        {
            m_date = date;
            m_begin = begin;
            m_end = end;
        }

        public override string ToString()
        {
            string date = m_date.ToShortDateString();
            string timeFormat = "{0,2:00}:{1,2:00}";
            string begin = String.Format(timeFormat, m_begin.Hours, m_begin.Minutes);
            string end = String.Format(timeFormat, m_end.Hours, m_end.Minutes);

            return date + " " + begin + " - " + end;
        }       
        public override int GetHashCode()
        {
            int hashCode = 0;
            unchecked
            {
                hashCode = m_begin.GetHashCode();
                hashCode += 373 * m_end.GetHashCode();
                hashCode += 373 * m_date.GetHashCode();
            }
            return hashCode;
        }
        
        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(null,obj))
            {
                return false;
            }

            if (Object.ReferenceEquals(this, obj))
            {
                return true;
            }

            ClassTime other = obj as ClassTime;
            if (!Object.ReferenceEquals(null, other))
            {
                return Equals(other);
            }

            return false;
        }
        public bool Equals(ClassTime other)
        {
            if (Object.ReferenceEquals(null, other))
            {
                return false;
            }

            if (Object.ReferenceEquals(this, other))
            {
                return true;
            }

            return Begin == other.Begin &&
                    End == other.End &&
                    Date == other.Date;            
        }

        public int CompareTo(Object other)
        {
            return CompareTo((ClassTime)other);
        }
        public int CompareTo(ClassTime other)
        {
            if (Date != other.Date)
            {
                return Date.CompareTo(other.Date);
            }

            if (Begin != other.Begin)
            {
                return Begin.CompareTo(other.Begin);
            }

            return End.CompareTo(other.End);
        }

        public static bool operator ==(ClassTime left, ClassTime right)
        {
            if (Object.ReferenceEquals(left, right))
            {
                return true;
            }

            if (Object.ReferenceEquals(left, null) ||
                Object.ReferenceEquals(right, null))
            {
                return false;
            }

            return left.Equals(right);
        }
        public static bool operator !=(ClassTime left, ClassTime right)
        {
            return !(left == right);
        }
        public static bool operator <(ClassTime left, ClassTime right)
        {
            return left.CompareTo(right) < 0;
        }
        public static bool operator <=(ClassTime left, ClassTime right)
        {
            return left.CompareTo(right) <= 0;
        }
        public static bool operator >(ClassTime left, ClassTime right)
        {
            return left.CompareTo(right) > 0;
        }
        public static bool operator >=(ClassTime left, ClassTime right)
        {
            return left.CompareTo(right) >= 0;
        }
    }
}
